﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class news_EditFirst : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string newsId = Request["newsId"];
            if (string.IsNullOrEmpty(newsId))
            {
                bd();
            }
            else
            {
                bd(newsId);
            }
        }
    }

    void bd(string newsId)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        long id=0;
        if(!long.TryParse(newsId,out id))
        {
            ClientScript.RegisterStartupScript(GetType(),"aa","alert('新闻编号错误')",true);
            return;
        }
        if (!dal.News.Any(x => x.id == id))
        {
            ClientScript.RegisterStartupScript(GetType(), "bb", "alert('新闻不存在')", true);
            return;
        }
        string moduleid = Request["id"];
        var c = dal.HeadlineContent.Where(x => x.ID == moduleid).First();
        lblModuleName.Text = c.Name;
        var news = dal.News.First(x => x.id == id);
        if (news.IsEnable == false)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('这篇新闻还没有发布')", true);
            return;
        }
        if (news.ptime > DateTime.Now)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('这篇新闻还没有到发布时间')", true);
            return;
        }
        txtTitle.Text = news.title;
        //85

        txtContent.Text = news.Summary;
        txtUrl.Text = string.Format(System.Configuration.ConfigurationManager.AppSettings["NewsUrl"], news.id);
    }

    protected void bd()
    {
        string id = Request["id"];
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var c = dal.HeadlineContent.Where(x => x.ID == id).First();
        lblModuleName.Text = c.Name;
        if (dal.HeadlineContent.Any(x => x.ID == id))
        {
            var tt = dal.HeadlineContent.First(x => x.ID == id);
            if (string.IsNullOrEmpty(tt.Content))
            {
                return;
            }
            FirstNews temp = Newtonsoft.Json.JsonConvert.DeserializeObject<FirstNews>(tt.Content);
            txtTitle.Text = temp.Title;
            txtContent.Text = temp.Content;
            txtUrl.Text = temp.Url;
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        string id = Request["id"];
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.HeadlineContent news = dal.HeadlineContent.First(x => x.ID == id);
        FirstNews fnews = new FirstNews();
        fnews.Title = txtTitle.Text;
        fnews.Content = txtContent.Text;
        fnews.Url = txtUrl.Text;
        string temp = Newtonsoft.Json.JsonConvert.SerializeObject(fnews);
        news.Content = temp;
    
        try
        {  
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "a", "alert('修改成功');document.location='EditFirstNews.aspx'", true);
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(GetType(), "a", "alert('添加失败');", true);
        }
    }
}